package component;

import enity.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
//实现BeanNameAware接口，实现通知
public class TeacherComponent implements BeanNameAware, BeanPostProcessor {
    @Autowired
    public User user;
    /*
    * 初始化的通知方法
    * */
    @Override
    public void setBeanName(String s) {
        System.out.println("实现了通知，Bean的名字是：" + s);
    }
    /*
    * 初始化中的前置方法
    * */
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化前置方法");
        return bean;
    }

    /*
    * 执行初始化方法
    * */
    @PostConstruct
    public void init() {
        System.out.println("执行注解方式的初始化方法");
    }

    /*
    * 执行初始化后置方法
    * */
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("执行初始化后置方法");
        return bean;
    }

    public void sayhi() {
        System.out.println("使用Bean,hi");
    }

    /*销毁Bean*/
    @PreDestroy
    public void destory() {
        System.out.println("销毁了Bean");
    }
}
